<?php

function paging($url, $rows_per_page, $pages_per_page, $num_rows, $page, $extra)
{	
	$numPages = ceil($num_rows/$rows_per_page);
	$offset = ($page-1)*$rows_per_page;
	$haft_limit = floor($pages_per_page/2);
	
	$list_page = "";
	if ($numPages > 1) 
	{ 
		$lowLimit = 0;
		$hiLimit = 0;
		
		if($page - $haft_limit < 1)
		{
			$lowLimit = 1;
			$highLimit = $numPages < $pages_per_page ? $numPages : ($lowLimit + ($pages_per_page-1));
		}
		else
		{
			$highLimit = $page+$haft_limit < $numPages ? $page+$haft_limit : $numPages;
			$lowLimit = $highLimit - ($pages_per_page-1);
			if($lowLimit < 1)
				$lowLimit = 1;
		}			
		
		
		if($page > 1)
		{
			$list_page .= "<span class='nav'><a href='$url?page=1$extra'><b>Đầu</b></a></span>"; 
			$prev = $page - 1;
			$list_page .= "<span class='nav'><a href='$url?page={$prev}$extra'><b>Trước</b></a></span>"; 
		}
		else
		{
			$list_page .= "<span class='nav'>Đầu</span>";
			$list_page .= "<span class='nav'>Trước</span>"; 
		}
		
		for ( $i = $lowLimit; $i <= $highLimit; $i++ ) 
		{ 
			if($i == $page)	//trang đang chọn
				$list_page .= "<span class='active nav'><b>{$page}</b></span>"; 				
			else
				$list_page .= "<span class='nav'><a href=\"$url?page={$i}$extra\">{$i}</a></span>"; 				
		}
		
		if($page < $numPages)
		{
			$next = $page + 1;
			$list_page .= "<span class='nav'><a href=\"$url?page={$next}$extra\"><b>Tiếp</b></a></span>"; 
			$list_page .= "<span class='nav'><a href='$url?page={$numPages}$extra'><b>Cuối</b></a></span>"; 
		}
		else
		{
			$list_page .= "<span class='nav'>Tiếp</span>"; 
			$list_page .= "<span class='nav'>Cuối</span>"; 		
		}		
	}  
	
	return "<div class='paging center'>$list_page</div>";
}
?>